home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / MENU.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  470 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.10  $
  6. //
  7. // Definition of Window Menu encapsulation class
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_MENU_H)
  10. #define OWL_MENU_H
  11.  
  12. #if !defined(OWL_GDIOBJEC_H)
  13. # include <owl/gdiobjec.h>
  14. #endif
  15.  
  16. #if defined(BI_NAMESPACE)
  17. namespace OWL {
  18. #endif
  19.  
  20. // Generic definitions/compiler options (eg. alignment) preceeding the 
  21. // definition of classes
  22. #include <services/preclass.h>
  23.  
  24. class _OWLCLASS_RTL TXMenu;
  25. class _OWLCLASS     TMenu;
  26.  
  27. #if 0  
  28. //
  29. // class TMenuItem
  30. // ~~~~~ ~~~~~~~~~
  31. // Representation of a menu item which can be a uint Id/position or a popup
  32. // menu handle.
  33. //
  34. class TMenuItem {
  35.   public:
  36.     TMenuItem(uint id) {Item = id;}
  37.     TMenuItem(HMENU popup) {Item = reinterpret_cast<uint>(popup);}
  38.     operator uint() {return Item;}
  39.  
  40.   private:
  41.     uint Item;
  42. };
  43. #else
  44. // Alias of an unsigned integer which identifies a menu item.
  45. //
  46. typedef uint TMenuItem;
  47. #endif
  48.  
  49. #if defined(BI_PLAT_WIN32)
  50.  
  51. // All-including mask for MENUITEMINFO
  52. //
  53. #if !defined(MIIM_ALL)
  54. # define MIIM_ALL  (MIIM_CHECKMARKS|MIIM_DATA|MIIM_ID|MIIM_STATE|MIIM_SUBMENU|MIIM_TYPE)
  55. #endif
  56.  
  57. //
  58. // class TMenuItemInfo
  59. // ~~~~~ ~~~~~~~~~~~~~
  60. class _OWLCLASS TMenuItemInfo : public MENUITEMINFO {
  61.   public:
  62.     TMenuItemInfo(uint msk = 0);
  63.  
  64.     // Construct MENUITEMINFO from a specific item of a menu object.
  65.     // Defaults to retrieving all information except for the type, which
  66.     // require a destination buffer.
  67.     //
  68.     TMenuItemInfo(TMenu& menu, uint posOrId, bool isPos, 
  69.                   uint msk = MIIM_CHECKMARKS|MIIM_DATA|
  70.                              MIIM_ID|MIIM_STATE|MIIM_SUBMENU);
  71.  
  72.     // Construct MENUITEMINFO from a specific item of a menu object.
  73.     // Similar to prior except that it also retrieves the type data.
  74.     //
  75.     TMenuItemInfo(TMenu& menu, uint posOrId, bool isPos, void* buffer, 
  76.                   uint size, uint msk = MIIM_ALL);
  77. };
  78.  
  79. #endif  
  80.  
  81. //
  82. // class TMenu
  83. // ~~~~~ ~~~~~
  84. class _OWLCLASS TMenu {
  85.   public:
  86.     TMenu(TAutoDelete autoDelete = AutoDelete);
  87.     TMenu(const TMenu& original, TAutoDelete autoDelete = AutoDelete);
  88.     TMenu(HMENU handle, TAutoDelete autoDelete = NoAutoDelete);
  89.     TMenu(HWND hWnd, TAutoDelete autoDelete = NoAutoDelete);
  90.     TMenu(const void far* menuTemplate);
  91.     TMenu(HINSTANCE instance, TResId resId);
  92.     virtual ~TMenu();
  93.  
  94.     TMenu& operator =(const TMenu&);
  95.  
  96.     void        CheckValid(uint redId = IDS_MENUFAILURE);
  97.     virtual HMENU GetHandle() const;
  98.  
  99.     operator    uint() const;
  100.     operator    HMENU() const;
  101.  
  102.     bool        IsOK() const;
  103.  
  104.     // HMENU encapsulated functions
  105.     //
  106.     bool        AppendMenu(uint flags, TMenuItem newItem=-1, const char far* newStr=0);
  107.     bool        AppendMenu(uint flags, TMenuItem newitem, const TBitmap& newBmp);
  108.     bool        CheckMenuItem(TMenuItem item, uint check);
  109.     bool        DeleteMenu(TMenuItem item, uint flags);
  110.     bool        EnableMenuItem(TMenuItem item, uint enable);
  111.     uint        GetMenuItemCount() const;
  112.     uint        GetMenuItemID(int posItem) const;
  113.     uint        GetMenuState(TMenuItem item, uint flags) const;
  114.     int         GetMenuString(TMenuItem item, char* str, int count, uint flags) const;
  115.     HMENU       GetSubMenu(int posItem) const;
  116.     bool        InsertMenu(TMenuItem item, uint flags, TMenuItem newItem=-1, const char far* newStr=0);
  117.     bool        InsertMenu(TMenuItem item, uint flags, TMenuItem newItem, const TBitmap& newBmp);
  118.     bool        ModifyMenu(TMenuItem item, uint flags, TMenuItem newItem=-1, const char far* newStr=0);
  119.     bool        ModifyMenu(TMenuItem item, uint flags, TMenuItem newItem, const TBitmap& newBmp);
  120.     bool        RemoveMenu(TMenuItem item, uint flags);
  121.     bool        SetMenuItemBitmaps(TMenuItem item, uint flags,
  122.                                    const TBitmap* bmpUnchecked=0,
  123.                                    const TBitmap* bmpChecked=0);
  124.  
  125.     // Encapsulation of new functionality introduced with Win95
  126.     // (Emulated when running on earlier versions of Windows)
  127.     //
  128.     uint        GetDefaultItem(bool getPos, uint flags) const;
  129.     bool        SetDefaultItem(uint posOrId, bool isPos);
  130.     bool        CheckRadioItem(uint first, uint last, uint check, uint flags);
  131.  
  132. #if defined(BI_PLAT_WIN32)
  133.  
  134.     // MENUITEMINFO related API
  135.     //
  136.     bool        GetMenuItemInfo(uint posOrId, bool isPos, TMenuItemInfo& mi) const;
  137.     bool        SetMenuItemInfo(uint posOrId, bool isPos, TMenuItemInfo& mi);
  138.     bool        InsertMenuItem(uint posOrId, bool isPos, TMenuItemInfo& mi);
  139. #endif
  140.  
  141.     static bool  GetMenuCheckMarkDimensions(TSize& size);
  142.     static TSize GetMenuCheckMarkDimensions();
  143.  
  144.     // Virtual menu functions
  145.     //
  146.     virtual void  MeasureItem(MEASUREITEMSTRUCT far& measureItem);
  147.     virtual void  DrawItem(DRAWITEMSTRUCT far& drawItem);
  148.  
  149. #if defined(OWL2_COMPAT)
  150.     typedef ::TXMenu TXMenu;  // Exceptions moved to global scope
  151. #endif
  152.  
  153.   protected:
  154.     static void DeepCopy(TMenu& dest, const TMenu& source, int offset = 0, int count = -1);
  155.     static void DeepCopy(TMenu& dst, int dstOff, const TMenu& src, int srcOff = 0, int count = -1);
  156.  
  157.   protected_data:
  158.     HMENU       Handle;
  159.     bool        ShouldDelete;   // Should this object destroy Menu?
  160. };
  161.  
  162. //
  163. // class TXMenu
  164. // ~~~~~ ~~~~~~
  165. class _OWLCLASS_RTL TXMenu : public TXOwl {
  166.   public:
  167.     TXMenu(uint resId = IDS_MENUFAILURE);
  168.  
  169. #if defined(BI_NO_COVAR_RET)
  170.     TXBase* Clone();
  171. #else
  172.     TXMenu* Clone();
  173. #endif
  174.     void Throw();
  175.  
  176.     static void Raise(uint resId = IDS_MENUFAILURE);
  177. };
  178.  
  179. //
  180. // class TSystemMenu
  181. // ~~~~~ ~~~~~~~~~~~
  182. class _OWLCLASS TSystemMenu : public TMenu {
  183.   public:
  184.     TSystemMenu(HWND wnd, bool revert= false);
  185.  
  186.   private:
  187.     TSystemMenu();
  188.     TSystemMenu(const TSystemMenu&);
  189. };
  190.  
  191. //
  192. // class TPopupMenu
  193. // ~~~~~ ~~~~~~~~~~
  194. class _OWLCLASS TPopupMenu : public TMenu {
  195.   public:
  196.     TPopupMenu(TAutoDelete autoDelete = AutoDelete);
  197.     TPopupMenu(HMENU handle, TAutoDelete autoDelete = NoAutoDelete);
  198.     TPopupMenu(const TMenu& menu, TAutoDelete autoDelete = AutoDelete);
  199.     bool        TrackPopupMenu(uint flags, int x, int y, int rsvd, HWND wnd,
  200.                                const TRect* rect=0);
  201.     bool        TrackPopupMenu(uint flags, const TPoint& point, int rsvd,
  202.                                HWND wnd, const TRect* rect=0);
  203.  
  204.   private:
  205.     TPopupMenu(const TPopupMenu&);
  206. };
  207.  
  208. //
  209. // class TMenuDescr
  210. // ~~~~~ ~~~~~~~~~~
  211. // Menu with information used to allow merging
  212. //
  213. class _OWLCLASS TMenuDescr : public TMenu {
  214.   public:
  215.     enum TGroup {
  216.       FileGroup,
  217.       EditGroup,
  218.       ContainerGroup,
  219.       ObjectGroup,
  220.       WindowGroup,
  221.       HelpGroup,
  222.       NumGroups
  223.     };
  224.     TMenuDescr();
  225.     TMenuDescr(const TMenuDescr& original);
  226.     TMenuDescr(TResId id, int fg, int eg, int cg, int og, int wg, int hg,
  227.                TModule* module = ::Module);
  228.     TMenuDescr(TResId id, TModule* module = ::Module);
  229.     TMenuDescr(HMENU hMenu, int fg, int eg, int cg, int og, int wg, int hg,
  230.                TModule* module = ::Module);
  231.    ~TMenuDescr();
  232.  
  233.     TMenuDescr& operator =(const TMenuDescr& original);
  234.  
  235.     HMENU    GetHandle() const;
  236.     TModule* GetModule() const;
  237.     void     SetModule(TModule* module);
  238.     TResId   GetId() const;
  239.     int      GetGroupCount(int group) const;
  240.     void     ClearServerGroupCount();
  241.     void     ClearContainerGroupCount();
  242.  
  243.     bool     Merge(const TMenuDescr& sourceMenuDescr);
  244.     bool     Merge(const TMenuDescr& sourceMenuDescr, TMenu& destMenu);
  245.  
  246.   protected:
  247.     bool     ExtractGroups();
  248.  
  249.   protected_data:
  250.     TModule* Module;       // module where menu lives
  251.     TResId   Id;           // resource id of menu
  252.     int      GroupCount[NumGroups];
  253.  
  254.   friend ipstream& operator >>(ipstream& is, TMenuDescr& m);
  255.   friend opstream& operator <<(opstream& os, const TMenuDescr& m);
  256. };
  257.  
  258. // Generic definitions/compiler options (eg. alignment) following the 
  259. // definition of classes
  260. #include <services/posclass.h>
  261.  
  262. #if defined(BI_NAMESPACE)
  263. } // namespace OWL
  264. #endif
  265.  
  266. //----------------------------------------------------------------------------
  267. // Inline implementations
  268. //
  269.  
  270. //
  271. inline HMENU TMenuDescr::GetHandle() const
  272. {
  273.   return Handle;
  274. }
  275.  
  276. //
  277. inline TModule* TMenuDescr::GetModule() const
  278. {
  279.   return Module;
  280. }
  281.  
  282. //
  283. inline void TMenuDescr::SetModule(TModule* module)
  284. {
  285.   Module = module;
  286. }
  287.  
  288. //
  289. inline TResId TMenuDescr::GetId() const
  290. {
  291.   return Id;
  292. }
  293.  
  294. //
  295. inline int TMenuDescr::GetGroupCount(int group) const
  296. {
  297.   return GroupCount[group];
  298. }
  299.  
  300. //
  301. inline HMENU TMenu::GetHandle() const
  302. {
  303.   return Handle;
  304. }
  305.  
  306. //
  307. inline TMenu::operator uint() const
  308. {
  309.   return uint(GetHandle());
  310. }
  311.  
  312. //
  313. inline TMenu::operator HMENU() const
  314. {
  315.   return GetHandle();
  316. }
  317.  
  318. //
  319. inline bool TMenu::IsOK() const
  320. {
  321.   return GetHandle() != 0;
  322. }
  323.  
  324. //
  325. inline bool TMenu::AppendMenu(uint flags, TMenuItem newItem, const char far* newStr)
  326. {
  327.   PRECONDITION(Handle);
  328.   return ::AppendMenu(Handle, flags, newItem, newStr);
  329. }
  330.  
  331. //
  332. inline bool TMenu::AppendMenu(uint flags, TMenuItem newItem, const TBitmap& newBmp)
  333. {
  334.   PRECONDITION(Handle);
  335.   return ::AppendMenu(Handle, flags|MF_BITMAP, newItem,
  336.          (const char far*)HBITMAP(newBmp));
  337. }
  338.  
  339. //
  340. inline bool TMenu::CheckMenuItem(TMenuItem item, uint check)
  341. {
  342.   PRECONDITION(Handle);
  343.   return ::CheckMenuItem(Handle, item, check);
  344. }
  345.  
  346. //
  347. inline bool TMenu::DeleteMenu(TMenuItem item, uint flags)
  348. {
  349.   PRECONDITION(Handle);
  350.   return ::DeleteMenu(Handle, item, flags);
  351. }
  352.  
  353. //
  354. inline bool TMenu::EnableMenuItem(TMenuItem item, uint enable)
  355. {
  356.   PRECONDITION(Handle);
  357.   return ::EnableMenuItem(Handle, item, enable);
  358. }
  359.  
  360. //
  361. inline uint TMenu::GetMenuItemCount() const
  362. {
  363.   PRECONDITION(Handle);
  364.   return ::GetMenuItemCount(Handle);
  365. }
  366.  
  367. //
  368. inline uint TMenu::GetMenuState(TMenuItem item, uint flags) const
  369. {
  370.   PRECONDITION(Handle);
  371.   return ::GetMenuState(Handle, item, flags);
  372. }
  373.  
  374. //
  375. inline int TMenu::GetMenuString(TMenuItem item, char* str, int count, uint flags) const
  376. {
  377.   PRECONDITION(Handle);
  378.   return ::GetMenuString(Handle, item, str, count, flags);
  379. }
  380.  
  381. //
  382. inline HMENU TMenu::GetSubMenu(int posItem) const
  383. {
  384.   PRECONDITION(Handle);
  385.   return ::GetSubMenu(Handle, posItem);
  386. }
  387.  
  388. //
  389. inline bool TMenu::InsertMenu(TMenuItem item, uint flags, TMenuItem newItem, const char far* newStr)
  390. {
  391.   PRECONDITION(Handle);
  392.   return ::InsertMenu(Handle, item, flags|MF_STRING, newItem, newStr);
  393. }
  394.  
  395. //
  396. inline bool TMenu::InsertMenu(TMenuItem item, uint flags, TMenuItem newItem, const TBitmap& newBmp)
  397. {
  398.   return ::InsertMenu(Handle, item, flags|MF_BITMAP, newItem,
  399.          (const char far*)HBITMAP(newBmp));
  400. }
  401.  
  402. //
  403. inline bool TMenu::ModifyMenu(TMenuItem item, uint flags, TMenuItem newItem, const char far* newStr)
  404. {
  405.   PRECONDITION(Handle);
  406.   return ::ModifyMenu(Handle, item, flags|MF_STRING, newItem, newStr);
  407. }
  408.  
  409. //
  410. inline bool TMenu::ModifyMenu(TMenuItem item, uint flags, TMenuItem newItem, const TBitmap& newBmp)
  411. {
  412.   PRECONDITION(Handle);
  413.   return ::ModifyMenu(Handle, item, flags|MF_BITMAP, newItem,
  414.          (const char far*)HBITMAP(newBmp));
  415. }
  416.  
  417. //
  418. inline bool TMenu::RemoveMenu(TMenuItem item, uint flags)
  419. {
  420.   PRECONDITION(Handle);
  421.   return ::RemoveMenu(Handle, item, flags);
  422. }
  423.  
  424. //
  425. inline bool TMenu::SetMenuItemBitmaps(TMenuItem item, uint flags,
  426.                                       const TBitmap* bmpUnchecked,
  427.                                       const TBitmap* bmpChecked)
  428. {
  429.   PRECONDITION(Handle);
  430.   return ::SetMenuItemBitmaps(Handle, item, flags,
  431.                               bmpUnchecked ? HBITMAP(*bmpUnchecked) : 0,
  432.                               bmpChecked ? HBITMAP(*bmpChecked) : 0);
  433. }
  434.  
  435. //
  436. // Returns true always.
  437. // The size reference will store the dimensions of the check mark bitmaps.
  438. //
  439. inline bool TMenu::GetMenuCheckMarkDimensions(TSize& size)
  440. {
  441.   size = ::GetMenuCheckMarkDimensions();
  442.   return true;
  443. }
  444.  
  445. //
  446. // Return the dimensions of the check mark bitmaps.
  447. //
  448. inline TSize TMenu::GetMenuCheckMarkDimensions()
  449. {
  450.   return ::GetMenuCheckMarkDimensions();
  451. }
  452.  
  453. //
  454. inline bool TPopupMenu::TrackPopupMenu(uint flags, int x, int y, int rsvd,
  455.                                        HWND wnd, const TRect* rect)
  456. {
  457.   PRECONDITION(Handle);
  458.   return ::TrackPopupMenu(Handle, flags, x, y, rsvd, wnd, rect);
  459. }
  460.  
  461. //
  462. inline bool TPopupMenu::TrackPopupMenu(uint flags, const TPoint& point,
  463.                                        int rsvd, HWND wnd, const TRect* rect)
  464. {
  465.   PRECONDITION(Handle);
  466.   return ::TrackPopupMenu(Handle, flags, point.x, point.y, rsvd, wnd, rect);
  467. }
  468.  
  469. #endif  // OWL_MENU_H
  470.